package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.lb.jwitter.util.ThoiKhoaBieuPhongNgayMonHoc;

import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuDuKien;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThuc;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThucService;
import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuan;
import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuanService;

import com.lb.jwitter.service.TblQLDTTXADMPhong;
import com.lb.jwitter.service.TblQLDTTXADMPhongService;

import com.lb.jwitter.service.TblQLDTTXADMTietHoc;
import com.lb.jwitter.service.TblQLDTTXADMTietHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAThoiKhoaBieuChinhThucSaveAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private Long tietBatDauId;

    
    private Long ngayId; 
    public void setNgayId(Long ngayId) {
       this.ngayId = ngayId;
 }

    public Long getNgayId() {
       return ngayId;
 }

    private TblQLDTTXADMNgayTrongTuanService tblQLDTTXADMNgayTrongTuanService; 

    private Long phongId; 
    public void setPhongId(Long phongId) {
       this.phongId = phongId;
 }

    public Long getPhongId() {
       return phongId;
 }

    private TblQLDTTXADMPhongService tblQLDTTXADMPhongService; 

    private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService; 

    private Long hocKyId; 
    public void setHocKyId(Long hocKyId) {
       this.hocKyId = hocKyId;
 }

    public Long getHocKyId() {
       return hocKyId;
 }

    private int maLoi; 
    public void setMaLoi(int maLoi) {
       this.maLoi = maLoi;
 }

    public int getMaLoi() {
       return maLoi;
 }
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private Long lopMonHocId; 


    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 

    private int daMoLop; 
    public void setDaMoLop(int daMoLop) {
       this.daMoLop = daMoLop;
 }

    public int getDaMoLop() {
       return daMoLop;
 }
    private int soTiet;

    private TblQLDTTXAThoiKhoaBieuChinhThuc tblQLDTTXAThoiKhoaBieuChinhThuc; 
    public TblQLDTTXAThoiKhoaBieuChinhThuc  getTblQLDTTXAThoiKhoaBieuChinhThuc() {
        return tblQLDTTXAThoiKhoaBieuChinhThuc;
 }

    private TblQLDTTXAThoiKhoaBieuChinhThucService tblQLDTTXAThoiKhoaBieuChinhThucService; 
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;

	private int preInsert(Message msg, TblQLDTTXAThoiKhoaBieuChinhThuc obj ){
		
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAThoiKhoaBieuChinhThuc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_ThoiKhoaBieuChinhThuc", "them")){ 
	         return "noprivilege"; 
	     }
	    
	     
        tblQLDTTXAThoiKhoaBieuChinhThucService = (TblQLDTTXAThoiKhoaBieuChinhThucService) context.getBean("tbl_QLDTTXA_ThoiKhoaBieuChinhThucClient"); 
        tblQLDTTXADMNgayTrongTuanService = (TblQLDTTXADMNgayTrongTuanService) context.getBean("tbl_QLDTTXA_DM_NgayTrongTuanClient"); 

        tblQLDTTXADMPhongService = (TblQLDTTXADMPhongService) context.getBean("tbl_QLDTTXA_DM_PhongClient"); 

        tblQLDTTXADMTietHocService = (TblQLDTTXADMTietHocService) context.getBean("tbl_QLDTTXA_DM_TietHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient");
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService)context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");

        msg = new Message();
        
        //Xoa cac lcih TKB truoc day trong cac cell 
        List<ConditionReport> cons = new ArrayList<ConditionReport> ();
        Long tietHocId = tietBatDauId;
        for (int i=0; i<soTiet; i++){
        	cons.clear();
  		     if(hocKyId != null && !hocKyId.equals(new Long(0))){
  		    	ConditionReport con = new ConditionReport();
  		    	con.setCol("hocKyId.id");
  		    	con.setValue1(hocKyId);
  		    	con.setOperator("eq");
  		    	cons.add(con);
  		     }else {
  		    	  return Action.ERROR;
  		     }
  	
  		     if(phongId != null && !phongId.equals(new Long(0))){
  		    	ConditionReport con = new ConditionReport();
  		    	con.setCol("phongId.id");
  		    	con.setValue1(phongId);
  		    	con.setOperator("ne");
  		    	cons.add(con);
  		     }else {
  		    	  return Action.ERROR;
  		     }
  		     if(ngayId != null && !ngayId.equals(new Long(0))){
  		    	ConditionReport con = new ConditionReport();
  		    	con.setCol("ngayId.id");
  		    	con.setValue1(ngayId);
  		    	con.setOperator("eq");
  		    	cons.add(con);
  		     }else {
  		    	  return Action.ERROR;
  		     }
  	
  		     if(tietHocId != null && !tietHocId.equals(new Long(0))){
  		    	ConditionReport con = new ConditionReport();
  		    	con.setCol("tietHocId.id");
  		    	con.setValue1(tietHocId);
  		    	con.setOperator("eq");
  		    	cons.add(con);
  		     }else {
  		    	  return Action.ERROR;
  		     }
  		   List<TblQLDTTXAThoiKhoaBieuChinhThuc> tblTKBChinhThucs = tblQLDTTXAThoiKhoaBieuChinhThucService.findtblQLDTTXAThoiKhoaBieuChinhThucByCondition(cons, 0, 0);
  		   
  		   cons.clear();
  		   ConditionReport con = new ConditionReport();
	    	con.setCol("lopMonHocId.id");
	    	con.setValue1(new Long(lopMonHocId));
	    	con.setOperator("eq");
	    	cons.add(con);
  		   List<TblQLDTTXALopMonHocHocVien> tblLopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
  		   
  		 if(tblTKBChinhThucs != null || tblTKBChinhThucs.size() > 0){
		    	 
		    	 for(TblQLDTTXAThoiKhoaBieuChinhThuc tblTKBChinhThuc : tblTKBChinhThucs){
			    	 TblQLDTTXALopMonHoc lopMonHoc = tblTKBChinhThuc.getLopMonHocId();
			    	 
			    	if(lopMonHoc.getGiaoVienId().getId().equals(tblQLDTTXALopMonHocService.gettblQLDTTXALopMonHoc(new Long(lopMonHocId)).getGiaoVienId().getId()) ){
						    	
			    		 // trung gio giao vien
			    		 maLoi = 1;
			    		 return Action.SUCCESS;
			    	 }
			    	 for(TblQLDTTXALopMonHocHocVien hocVien : tblLopMonHocHocViens){
	  		    			cons.clear();
	  		    			ConditionReport con4 = new ConditionReport();
	  		  		    	con4.setCol("lopMonHocId.id");
	  		  		    	con4.setValue1(tblTKBChinhThuc.getLopMonHocId().getId());
	  		  		    	con4.setOperator("eq");
	  		  		    	cons.add(con4);
	  		  		    	
	  		  		    	ConditionReport con5 = new ConditionReport();
			  		    	con5.setCol("hocVienId.id");
			  		    	con5.setValue1(hocVien.getHocVienId().getId());
			  		    	con5.setOperator("eq");
			  		    	cons.add(con5);
			  		    	
			  		    	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons);
			  		    	if(num > 0){
			  		    		//bao loi trung TKB hoc vien
			  		    		maLoi = 2;
			  		    		return Action.SUCCESS;
			  		    	}
	  		    		}
			     }
			    	 
		     }  
  		     
  		     
        	//tietHocId = tblQLDTTXADMTietHocService.gettblQLDTTXADMTietHoc(new Long(tietHocId)).getTietKeTiep().getId();
        	TblQLDTTXADMTietHoc tietKeTiep = tblQLDTTXADMTietHocService.gettblQLDTTXADMTietHoc(new Long(tietHocId)).getTietKeTiep();
     		 if (tietKeTiep != null){
           	tietHocId = tietKeTiep.getId();
     		 } else {
     			 break;
     		 }
        }
        tietHocId = tietBatDauId;
        for(int i =0; i < soTiet; i++){
   			 
        	cons.clear();
   		     if(hocKyId != null && !hocKyId.equals(new Long(0))){
   		    	ConditionReport con = new ConditionReport();
   		    	con.setCol("hocKyId.id");
   		    	con.setValue1(hocKyId);
   		    	con.setOperator("eq");
   		    	cons.add(con);
   		     }else {
   		    	  return Action.ERROR;
   		     }
   	
   		     if(phongId != null && !phongId.equals(new Long(0))){
   		    	ConditionReport con = new ConditionReport();
   		    	con.setCol("phongId.id");
   		    	con.setValue1(phongId);
   		    	con.setOperator("eq");
   		    	cons.add(con);
   		     }else {
   		    	  return Action.ERROR;
   		     }
   		     if(ngayId != null && !ngayId.equals(new Long(0))){
   		    	ConditionReport con = new ConditionReport();
   		    	con.setCol("ngayId.id");
   		    	con.setValue1(ngayId);
   		    	con.setOperator("eq");
   		    	cons.add(con);
   		     }else {
   		    	  return Action.ERROR;
   		     }
   	
   		     if(tietHocId != null && !tietHocId.equals(new Long(0))){
   		    	ConditionReport con = new ConditionReport();
   		    	con.setCol("tietHocId.id");
   		    	con.setValue1(tietHocId);
   		    	con.setOperator("eq");
   		    	cons.add(con);
   		     }else {
   		    	  return Action.ERROR;
   		     }
   		 		     
   		     List<TblQLDTTXAThoiKhoaBieuChinhThuc> tblTKBDuKiens = tblQLDTTXAThoiKhoaBieuChinhThucService.findtblQLDTTXAThoiKhoaBieuChinhThucByCondition(cons, 0, 0);
   		     
   		     if(tblTKBDuKiens != null || tblTKBDuKiens.size() > 0){
   		    	 
   		    	 for(TblQLDTTXAThoiKhoaBieuChinhThuc tblTKBDuKien : tblTKBDuKiens){
   			    	 tblQLDTTXAThoiKhoaBieuChinhThucService.deletetblQLDTTXAThoiKhoaBieuChinhThuc(tblTKBDuKien.getId());
   			    	 //tietHocId = tblTKBDuKien.getTietHocId().getTietKeTiep().getId();
   			    	TblQLDTTXADMTietHoc tietKeTiep = tblTKBDuKien.getTietHocId().getTietKeTiep();
	   		  		 if (tietKeTiep != null){
	   		        	tietHocId = tietKeTiep.getId();
	   		  		 } else {
	   		  			 break;
	   		  		 }
   			     }
   			    	 
   		     }  // end-if tkbDuKiens != null	
        }// end-for i to soTiet
        
        tietHocId = tietBatDauId;
	    for(int i =0 ; i < soTiet ; i++){
	        tblQLDTTXAThoiKhoaBieuChinhThuc = new TblQLDTTXAThoiKhoaBieuChinhThuc(); 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setNgayId(tblQLDTTXADMNgayTrongTuanService.gettblQLDTTXADMNgayTrongTuan(new Long(ngayId))); 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setPhongId(tblQLDTTXADMPhongService.gettblQLDTTXADMPhong(new Long(phongId))); 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setTietHocId(tblQLDTTXADMTietHocService.gettblQLDTTXADMTietHoc(new Long(tietHocId))); 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId))); 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setLopMonHocId(tblQLDTTXALopMonHocService.gettblQLDTTXALopMonHoc(new Long(lopMonHocId))); 

	        
	        preInsert(msg, tblQLDTTXAThoiKhoaBieuChinhThuc );
	        if (msg.isPreMessage_b()) 
	        { 
	           id = tblQLDTTXAThoiKhoaBieuChinhThucService.createtblQLDTTXAThoiKhoaBieuChinhThuc(tblQLDTTXAThoiKhoaBieuChinhThuc); 
	           postInsert(msg, tblQLDTTXAThoiKhoaBieuChinhThuc); 
	           if (msg.isPostMessage_b() == false) 
	           { 
	               return Action.ERROR; 
	           } 
	        }else{ 
	           return Action.ERROR; 
	        } 
	        tblQLDTTXAThoiKhoaBieuChinhThuc.setId(id);
	        
	        //tietHocId = tblQLDTTXAThoiKhoaBieuChinhThuc.getTietHocId().getTietKeTiep().getId();
	        TblQLDTTXADMTietHoc tietKeTiep = tblQLDTTXAThoiKhoaBieuChinhThuc.getTietHocId().getTietKeTiep();
	  		 if (tietKeTiep != null){
	        	tietHocId = tietKeTiep.getId();
	  		 } else {
	  			 break;
	  		 }
	    }   
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_ThoiKhoaBieuChinhThuc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public Long getId() {
	return id;
}

public Long getTietBatDauId() {
	return tietBatDauId;
}

public void setTietBatDauId(Long tietBatDauId) {
	this.tietBatDauId = tietBatDauId;
}

public int getSoTiet() {
	return soTiet;
}

public void setSoTiet(int soTiet) {
	this.soTiet = soTiet;
}

public Long getLopMonHocId() {
	return lopMonHocId;
}

public void setLopMonHocId(Long lopMonHocId) {
	this.lopMonHocId = lopMonHocId;
} 
 } 
 
